package com.example.gmr.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.gmr.R;

public class Mouse extends AppCompatActivity {

    Button btn_return;
    private int i=0;
    private ImageView mouse;
    private TextView info;
    private Handler handler;
    public int[][] position=new int[][]{
            {250,150},{600,150},{930,150},{210,300},{600,300},{930,300},{200,400},{600,440},{970,440}
    };
    private void btnonclickreturn(){
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Mouse.this,Login.class);
                startActivity(intent);
                finish();
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mouse);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        mouse=(ImageView)findViewById(R.id.imageView1);
        info=findViewById(R.id.info);
        btn_return=findViewById(R.id.btn_return);
        info.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
               switch (event.getAction()){
                   case MotionEvent.ACTION_DOWN:
                       float x=event.getRawX();
                       float y=event.getRawY();
                       Log.i("x:"+x,"y:"+y);
                       break;
                   default :
                       break;
               }
               return false;
            }
        });
        handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                int index;
                if(msg.what==0x101){
                    index=msg.arg1;
                    mouse.setX(position[index][0]);
                    mouse.setY(position[index][1]);
                    mouse.setVisibility(View.VISIBLE);
                }
                super.handleMessage(msg);
            }
        };
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
                int index=0;
                while (!Thread.currentThread().isInterrupted()){
                    index=new Random().nextInt(position.length);
                    Message m=handler.obtainMessage();
                    m.what=0x101;
                    m.arg1=index;
                    handler.sendMessage(m);
                    try{
                        Thread.sleep(new Random().nextInt(500)+500);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
        mouse.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.setVisibility(View.INVISIBLE);
                i++;
                Toast.makeText(Mouse.this,"打到【"+i+"】只地鼠!",Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        btnonclickreturn();
    }
}
